/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-api:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-manager:0.1-SNAPSHOT"
        ]
    }
*/

package org.helgoboss.commandlet_manager_ref_impl

import org.helgoboss.commandlet._
import org.helgoboss.osgi_additions._
import org.helgoboss.commandlet_manager.CommandletManager

class Activator extends OsgiContext with OsgiLogging with OsgiProvider with OsgiServiceWatcher {
    whenBundleActive {
        val commandletMap = new collection.mutable.HashMap[String, Commandlet]
        watchServices[Commandlet] {
            case AddingService(commandlet, _) => commandletMap(commandlet.id) = commandlet
            case RemovedService(commandlet, _) => commandletMap -= commandlet.id
        }
        new MyCommandletManager(commandletMap = commandletMap).providesService[CommandletManager]
    }

    class MyCommandletManager(commandletMap: collection.mutable.Map[String, Commandlet]) extends CommandletManager {
        def findCommandlet(commandletId: String) = {
            commandletMap.get(commandletId)
        }
    }
}